package com.pg_chat.utils;

import com.pg_chat.config.YmlProperties;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;

public abstract class SmsServiceUtil {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private YmlProperties ymlProperties;

    protected String smsUrl;
    protected String smsAccount;
    protected String smsPwd;

    protected void init(){
        YamlPropertiesLoader.yamlLoad("config.yaml");
        try {
            smsUrl = (String)YamlPropertiesLoader.getCommon("sms.smsUrl");
            smsAccount = (String)YamlPropertiesLoader.getCommon("sms.smsAccount");
            smsPwd = (String)YamlPropertiesLoader.getCommon("sms.smsPwd");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public abstract boolean send(String phone, String msg);

    public String httpPost(String url, String json) throws Exception{
        CloseableHttpClient client = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        //设置请求体头
        httpPost.addHeader("Content-Type", "application/json;charset=UTF-8");
        httpPost.setHeader("Accept","application/json");
        //设置请求体
        httpPost.setEntity(new StringEntity(json, StandardCharsets.UTF_8));
        //发送url请求,获取返回信息
        CloseableHttpResponse response = client.execute(httpPost);
        HttpEntity entity = response.getEntity();
        String responseContent = EntityUtils.toString(entity,"UTF-8");
        //关闭连接
        response.close();
        client.close();
        return responseContent;
    }

    /**
     *
     * @param respond
     * @param mobiles
     * @return
     */
    protected abstract Boolean state4Respond(String respond, String mobiles);

    /**
     *状态码转换
     * @param rspCode
     * @return
     */
    protected abstract String stateCode(String rspCode);

    protected abstract class SmsRequest{

        private String[] phone;
        private String msg;
        private String account;
        private String password;

        public SmsRequest() {
        }

        public String[] getPhone() {
            return phone;
        }

        public void setPhone(String[] phone) {
            this.phone = phone;
        }

        public String getMsg() {
            return msg;
        }

        public void setMsg(String msg) {
            this.msg = msg;
        }

        public String getAccount() {
            return account;
        }

        public void setAccount(String account) {
            this.account = account;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }

        public abstract String requestBody();

    }
}
